package org.semanticflash.rdf.io.auto
{

	import com.adobe.net.URI;
	import com.bigflexlib.core.markers.IStub;
	import com.bigflexlib.utils.AsyncRunner;
	
	import flash.events.EventDispatcher;
	
	import mx.core.Application;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.HTTPService;
	
	import org.semanticflash.rdf.io.RDFArrayHandler;
	import org.semanticflash.rdf.io.ntriple.NTripleParser;
	import org.semanticflash.rdf.io.rdfxml.RDFXMLParser;


/**
 * @eventType mx.rpc.events.ResultEvent.RESULT 
 */
[Event(name="result", type="mx.rpc.events.ResultEvent")]

/**
 * @eventType mx.rpc.events.FaultEvent.FAULT 
 */
[Event(name="fault", type="mx.rpc.events.FaultEvent")]


	/**
	 * 
	 * This class will shield the user from retrieval, negotiation and parsing mechanisms
	 * used behind the curtains.
	 * 
	 * We are using it as a main entry point to the io framework so we can gather different use-cases
	 * here and hopefully generalize.
	 * 
	 * @author aldo.bucchi@gmail.com
	 * 
	 */
	public class RDFAutoLoader extends EventDispatcher implements IStub
	{
		
		
		private var _url:String = null;
		public function get url( ):String
		{
			return _url;
		}
		
		
		private var _baseURL:String;
		public function get baseURL( ):String
		{
			return _baseURL;
		}
		
		
		
		private var _lastResult:Array;
		[Bindable("result")]
		public function get lastResult( ):Array
		{
			return _lastResult;
		}
		
		
		
		

		private var srv:HTTPService;
		
		
		
		
		public function RDFAutoLoader( url:String, baseURL:String = null )
		{
			
			_url = url;
			_baseURL = baseURL;
			
		}
		


		public function load( ):void
		{
		
		
			var uri:URI = new URI( _url );
			
			
			if ( uri.scheme == "http" || uri.scheme == "https" )
			{
				
				srv = new HTTPService( );
				
				// headers are not working. they depend on the crossdomain.xml policy
				// http://kb.adobe.com/selfservice/viewContent.do?externalId=kb403185&sliceId=2
//				srv.headers = {
//						Accept : "application/rdf+xml"
//					};
				
				
				srv.url = "http://triplr.org/rdf/" + _url.substr( _url.indexOf( ":" ) + 3 );
				srv.resultFormat = HTTPService.RESULT_FORMAT_E4X;			// only text, we will try to find out the actual format later
				srv.addEventListener( ResultEvent.RESULT, srv_result );
				srv.addEventListener( FaultEvent.FAULT, srv_fault );
				srv.send( );
				
			}
			else if ( uri.scheme == "file" )
			{
				
				// TODO
			
			}
			
						
		
		}
		
		
		
		
		private function srv_result( event:ResultEvent ):void
		{
		
		
			var triples:Array = [ ];
			var runner:AsyncRunner;

			// try to figure out the format
			
			var extension:String = new URI( _url ).getExtension( true );
			Application.application.ta.text += String( event.result ).substr( 0, 30 );
			var xml:XML  = null;
			try{ 
				xml = new XML( event.result );
			} catch (e:Error ){};
			

			if ( xml != null ) //( extension == "rdf" )
			{
				// create new RDFXMLParser
				var rdfp:RDFXMLParser = new RDFXMLParser( xml, new RDFArrayHandler( triples ), resolvedBaseURL );
				runner = rdfp.runner;
			}
			else if ( extension == "nt" )
			{
				
				var ntString:String = String( event.result );
				var ntp:NTripleParser = new NTripleParser( ntString, new RDFArrayHandler( triples ), resolvedBaseURL );
				
				runner = ntp.runner;
			}
			else
			{
				trace( "unhandled format... " );
				return;
			}
			
			
			
			runner.run( );
			
			var event:ResultEvent = new ResultEvent( ResultEvent.RESULT, false, true, triples );
			
			dispatchEvent( event );
			
		}
		
		
		private function srv_fault( event:FaultEvent ):void
		{
			dispatchEvent( new FaultEvent( FaultEvent.FAULT, false, false, event.fault ) );
		}
		
		
		
		
		
		
		
		
		
		
		
		private function get resolvedBaseURL( ):String
		{
		
			return _baseURL ? _baseURL : _url;
		
		}
		
		














		
		
	}
	
	
}


